<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>Inspector | Node.js v8.12.0 Documentation</title>
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic">
  <link rel="stylesheet" href="assets/style.css">
  <link rel="stylesheet" href="assets/sh.css">
  <link rel="canonical" href="https://nodejs.org/api/inspector.html">
</head>
<body class="alt apidoc" id="api-section-inspector">
  <div id="content" class="clearfix">
    <div id="column2" class="interior">
      <div id="intro" class="interior">
        <a href="/" title="Go back to the home page">
          Node.js
        </a>
      </div>
      <ul>
<li><a class="nav-documentation" href="documentation.html">About these Docs</a></li>
<li><a class="nav-synopsis" href="synopsis.html">Usage &amp; Example</a></li>
</ul>
<div class="line"></div>

<ul>
<li><a class="nav-assert" href="assert.html">Assertion Testing</a></li>
<li><a class="nav-async_hooks" href="async_hooks.html">Async Hooks</a></li>
<li><a class="nav-buffer" href="buffer.html">Buffer</a></li>
<li><a class="nav-addons" href="addons.html">C++ Addons</a></li>
<li><a class="nav-n-api" href="n-api.html">C/C++ Addons - N-API</a></li>
<li><a class="nav-child_process" href="child_process.html">Child Processes</a></li>
<li><a class="nav-cluster" href="cluster.html">Cluster</a></li>
<li><a class="nav-cli" href="cli.html">Command Line Options</a></li>
<li><a class="nav-console" href="console.html">Console</a></li>
<li><a class="nav-crypto" href="crypto.html">Crypto</a></li>
<li><a class="nav-debugger" href="debugger.html">Debugger</a></li>
<li><a class="nav-deprecations" href="deprecations.html">Deprecated APIs</a></li>
<li><a class="nav-dns" href="dns.html">DNS</a></li>
<li><a class="nav-domain" href="domain.html">Domain</a></li>
<li><a class="nav-esm" href="esm.html">ECMAScript Modules</a></li>
<li><a class="nav-errors" href="errors.html">Errors</a></li>
<li><a class="nav-events" href="events.html">Events</a></li>
<li><a class="nav-fs" href="fs.html">File System</a></li>
<li><a class="nav-globals" href="globals.html">Globals</a></li>
<li><a class="nav-http" href="http.html">HTTP</a></li>
<li><a class="nav-http2" href="http2.html">HTTP/2</a></li>
<li><a class="nav-https" href="https.html">HTTPS</a></li>
<li><a class="nav-inspector active" href="inspector.html">Inspector</a></li>
<li><a class="nav-intl" href="intl.html">Internationalization</a></li>
<li><a class="nav-modules" href="modules.html">Modules</a></li>
<li><a class="nav-net" href="net.html">Net</a></li>
<li><a class="nav-os" href="os.html">OS</a></li>
<li><a class="nav-path" href="path.html">Path</a></li>
<li><a class="nav-perf_hooks" href="perf_hooks.html">Performance Hooks</a></li>
<li><a class="nav-process" href="process.html">Process</a></li>
<li><a class="nav-punycode" href="punycode.html">Punycode</a></li>
<li><a class="nav-querystring" href="querystring.html">Query Strings</a></li>
<li><a class="nav-readline" href="readline.html">Readline</a></li>
<li><a class="nav-repl" href="repl.html">REPL</a></li>
<li><a class="nav-stream" href="stream.html">Stream</a></li>
<li><a class="nav-string_decoder" href="string_decoder.html">String Decoder</a></li>
<li><a class="nav-timers" href="timers.html">Timers</a></li>
<li><a class="nav-tls" href="tls.html">TLS/SSL</a></li>
<li><a class="nav-tracing" href="tracing.html">Tracing</a></li>
<li><a class="nav-tty" href="tty.html">TTY</a></li>
<li><a class="nav-dgram" href="dgram.html">UDP/Datagram</a></li>
<li><a class="nav-url" href="url.html">URL</a></li>
<li><a class="nav-util" href="util.html">Utilities</a></li>
<li><a class="nav-v8" href="v8.html">V8</a></li>
<li><a class="nav-vm" href="vm.html">VM</a></li>
<li><a class="nav-zlib" href="zlib.html">ZLIB</a></li>
</ul>
<div class="line"></div>

<ul>
<li><a class="nav-https-github-com-nodejs-node" href="https://github.com/nodejs/node">GitHub Repo &amp; Issue Tracker</a></li>
<li><a class="nav-https-groups-google-com-group-nodejs" href="https://groups.google.com/group/nodejs">Mailing List</a></li>
</ul>

    </div>

    <div id="column1" data-id="inspector" class="interior">
      <header>
        <h1>Node.js v8.12.0 Documentation</h1>
        <div id="gtoc">
          <ul>
            <li>
              <a href="index.html" name="toc">Index</a>
            </li>
            <li>
              <a href="all.html">View on single page</a>
            </li>
            <li>
              <a href="inspector.json">View as JSON</a>
            </li>
            
    <li class="version-picker">
      <a href="#">View another version <span>&#x25bc;</span></a>
      <ol class="version-picker"><li><a href="https://nodejs.org/docs/latest-v9.x/api/inspector.html">9.x</a></li></ol>
    </li>
  
          </ul>
        </div>
        <hr>
      </header>

      <div id="toc">
        <h2>Table of Contents</h2>
        <ul>
<li><span class="stability_1"><a href="#inspector_inspector">Inspector</a></span><ul>
<li><span class="stability_undefined"><a href="#inspector_inspector_open_port_host_wait">inspector.open([port[, host[, wait]]])</a></span><ul>
<li><span class="stability_undefined"><a href="#inspector_inspector_close">inspector.close()</a></span></li>
<li><span class="stability_undefined"><a href="#inspector_inspector_url">inspector.url()</a></span></li>
</ul>
</li>
<li><span class="stability_undefined"><a href="#inspector_class_inspector_session">Class: inspector.Session</a></span><ul>
<li><span class="stability_undefined"><a href="#inspector_constructor_new_inspector_session">Constructor: new inspector.Session()</a></span></li>
<li><span class="stability_undefined"><a href="#inspector_event_inspectornotification">Event: &#39;inspectorNotification&#39;</a></span></li>
<li><span class="stability_undefined"><a href="#inspector_event_lt_inspector_protocol_method_gt">Event: &lt;inspector-protocol-method&gt;</a></span></li>
<li><span class="stability_undefined"><a href="#inspector_session_connect">session.connect()</a></span></li>
<li><span class="stability_undefined"><a href="#inspector_session_post_method_params_callback">session.post(method[, params][, callback])</a></span></li>
<li><span class="stability_undefined"><a href="#inspector_session_disconnect">session.disconnect()</a></span></li>
</ul>
</li>
<li><span class="stability_undefined"><a href="#inspector_example_usage">Example usage</a></span><ul>
<li><span class="stability_undefined"><a href="#inspector_cpu_profiler">CPU Profiler</a></span></li>
</ul>
</li>
</ul>
</li>
</ul>

      </div>

      <div id="apicontent">
        <h1>Inspector<span><a class="mark" href="#inspector_inspector" id="inspector_inspector">#</a></span></h1>
<!--introduced_in=v8.0.0-->
<div class="api_stability api_stability_1"><a href="documentation.html#documentation_stability_index">Stability: 1</a> - Experimental</div><p>The <code>inspector</code> module provides an API for interacting with the V8 inspector.</p>
<p>It can be accessed using:</p>
<pre><code class="lang-js">const inspector = require(&#39;inspector&#39;);
</code></pre>
<h2>inspector.open([port[, host[, wait]]])<span><a class="mark" href="#inspector_inspector_open_port_host_wait" id="inspector_inspector_open_port_host_wait">#</a></span></h2>
<div class="signature"><ul>
<li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a> Port to listen on for inspector connections. Optional.
<strong>Default:</strong> what was specified on the CLI.</li>
<li><code>host</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> Host to listen on for inspector connections. Optional.
<strong>Default:</strong> what was specified on the CLI.</li>
<li><code>wait</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&lt;boolean&gt;</a> Block until a client has connected. Optional.
<strong>Default:</strong> <code>false</code>.</li>
</ul>
</div><p>Activate inspector on host and port. Equivalent to <code>node
--inspect=[[host:]port]</code>, but can be done programmatically after node has
started.</p>
<p>If wait is <code>true</code>, will block until a client has connected to the inspect port
and flow control has been passed to the debugger client.</p>
<h3>inspector.close()<span><a class="mark" href="#inspector_inspector_close" id="inspector_inspector_close">#</a></span></h3>
<p>Deactivate the inspector. Blocks until there are no active connections.</p>
<h3>inspector.url()<span><a class="mark" href="#inspector_inspector_url" id="inspector_inspector_url">#</a></span></h3>
<p>Return the URL of the active inspector, or <code>undefined</code> if there is none.</p>
<h2>Class: inspector.Session<span><a class="mark" href="#inspector_class_inspector_session" id="inspector_class_inspector_session">#</a></span></h2>
<p>The <code>inspector.Session</code> is used for dispatching messages to the V8 inspector
back-end and receiving message responses and notifications.</p>
<h3>Constructor: new inspector.Session()<span><a class="mark" href="#inspector_constructor_new_inspector_session" id="inspector_constructor_new_inspector_session">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v8.0.0</span>
</div><p>Create a new instance of the <code>inspector.Session</code> class. The inspector session
needs to be connected through <a href="#inspector_session_connect"><code>session.connect()</code></a> before the messages
can be dispatched to the inspector backend.</p>
<p><code>inspector.Session</code> is an <a href="events.html#events_class_eventemitter"><code>EventEmitter</code></a> with the following events:</p>
<h3>Event: &#39;inspectorNotification&#39;<span><a class="mark" href="#inspector_event_inspectornotification" id="inspector_event_inspectornotification">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v8.0.0</span>
</div><ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&lt;Object&gt;</a> The notification message object</li>
</ul>
<p>Emitted when any notification from the V8 Inspector is received.</p>
<pre><code class="lang-js">session.on(&#39;inspectorNotification&#39;, (message) =&gt; console.log(message.method));
// Debugger.paused
// Debugger.resumed
</code></pre>
<p>It is also possible to subscribe only to notifications with specific method:</p>
<h3>Event: &lt;inspector-protocol-method&gt;<span><a class="mark" href="#inspector_event_lt_inspector_protocol_method_gt" id="inspector_event_lt_inspector_protocol_method_gt">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v8.0.0</span>
</div><ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&lt;Object&gt;</a> The notification message object</li>
</ul>
<p>Emitted when an inspector notification is received that has its method field set
to the <code>&lt;inspector-protocol-method&gt;</code> value.</p>
<p>The following snippet installs a listener on the <a href="https://chromedevtools.github.io/devtools-protocol/v8/Debugger/#event-paused"><code>Debugger.paused</code></a>
event, and prints the reason for program suspension whenever program
execution is suspended (through breakpoints, for example):</p>
<pre><code class="lang-js">session.on(&#39;Debugger.paused&#39;, ({ params }) =&gt; {
  console.log(params.hitBreakpoints);
});
// [ &#39;/the/file/that/has/the/breakpoint.js:11:0&#39; ]
</code></pre>
<h3>session.connect()<span><a class="mark" href="#inspector_session_connect" id="inspector_session_connect">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v8.0.0</span>
</div><p>Connects a session to the inspector back-end. An exception will be thrown
if there is already a connected session established either through the API or by
a front-end connected to the Inspector WebSocket port.</p>
<h3>session.post(method[, params][, callback])<span><a class="mark" href="#inspector_session_post_method_params_callback" id="inspector_session_post_method_params_callback">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v8.0.0</span>
</div><ul>
<li>method <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
<li>params <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&lt;Object&gt;</a></li>
<li>callback <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&lt;Function&gt;</a></li>
</ul>
<p>Posts a message to the inspector back-end. <code>callback</code> will be notified when
a response is received. <code>callback</code> is a function that accepts two optional
arguments - error and message-specific result.</p>
<pre><code class="lang-js">session.post(&#39;Runtime.evaluate&#39;, { expression: &#39;2 + 2&#39; },
             (error, { result }) =&gt; console.log(result));
// Output: { type: &#39;number&#39;, value: 4, description: &#39;4&#39; }
</code></pre>
<p>The latest version of the V8 inspector protocol is published on the
<a href="https://chromedevtools.github.io/devtools-protocol/v8/">Chrome DevTools Protocol Viewer</a>.</p>
<p>Node inspector supports all the Chrome DevTools Protocol domains declared
by V8. Chrome DevTools Protocol domain provides an interface for interacting
with one of the runtime agents used to inspect the application state and listen
to the run-time events.</p>
<h3>session.disconnect()<span><a class="mark" href="#inspector_session_disconnect" id="inspector_session_disconnect">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v8.0.0</span>
</div><p>Immediately close the session. All pending message callbacks will be called
with an error. <a href="#inspector_session_connect"><code>session.connect()</code></a> will need to be called to be able to send
messages again. Reconnected session will lose all inspector state, such as
enabled agents or configured breakpoints.</p>
<h2>Example usage<span><a class="mark" href="#inspector_example_usage" id="inspector_example_usage">#</a></span></h2>
<h3>CPU Profiler<span><a class="mark" href="#inspector_cpu_profiler" id="inspector_cpu_profiler">#</a></span></h3>
<p>Apart from the debugger, various V8 Profilers are available through the DevTools
protocol. Here&#39;s a simple example showing how to use the <a href="https://chromedevtools.github.io/devtools-protocol/v8/Profiler">CPU profiler</a>:</p>
<pre><code class="lang-js">const inspector = require(&#39;inspector&#39;);

const session = new inspector.Session();
session.connect();

session.post(&#39;Profiler.enable&#39;, () =&gt; {
  session.post(&#39;Profiler.start&#39;, () =&gt; {
    // invoke business logic under measurement here...

    // some time later...
    session.post(&#39;Profiler.stop&#39;, ({ profile }) =&gt; {
      // write profile to disk, upload, etc.
    });
  });
});
</code></pre>

      </div>
    </div>
  </div>
  <script src="assets/sh_main.js"></script>
  <script src="assets/sh_javascript.min.js"></script>
  <script>highlight(undefined, undefined, 'pre');</script>
  <!-- __TRACKING__ -->
</body>
</html>

